# -*- coding: utf-8 -*-
"""
Created on Mon Jun  3 18:13:44 2019

@author: jlani
"""

from collections import OrderedDict
import csv
import pickle

#Process the price data. 
#Makes a dictionary whose keys are periods (1-26) and whose items are dictionaries
#whose items are the price of each different good.

price_dict = OrderedDict()

with open('price.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        del row['count']
        period = int( row.pop('Period') )
        for key in row.keys():
            row[key] = float( row[key] )
        price_dict[ period ] = row
    
with open('price.pickle','wb') as f:
    pickle.dump(price_dict, f)


#Process the quantity data.
#Make a dictionary whose keys are participant ids. The items are dictionaries
#whose keys are periods and whose items are dictionaries. The keys of these 
#dictionaries are the names of goods and the items are the quantities consumed.

cons_dict = OrderedDict()

with open('quantity.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        del row['count']
        panelistid = int( row.pop('Panelist') )
        period = int( row.pop('Period') )
        if panelistid not in cons_dict: cons_dict[panelistid] = OrderedDict()
        if period in cons_dict[panelistid]: raise Exception('Period should not be in cons dict')
        for key in row.keys():
            row[key] = float( row[key] )
        cons_dict[panelistid][period] = row

with open('cons.pickle','wb') as f:
    pickle.dump(cons_dict, f)




